package com.kongge.viewdisplayframeworklib.control;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import com.kongge.viewdisplayframeworklib.event.action.EQAction;
import com.kongge.viewdisplayframeworklib.event.action.EQGotoFrameAction;
import com.kongge.viewdisplayframeworklib.landscape.AbstractUIManager;
import com.kongge.viewdisplayframeworklib.model.AbsUIController;
import com.kongge.viewdisplayframeworklib.model.Page;
import com.kongge.viewdisplayframeworklib.model.PageControlStack;
import com.kongge.viewdisplayframeworklib.model.TabberBarItem;
import com.kongge.viewdisplayframeworklib.view.TabWidget;

import java.util.ArrayList;
import java.util.List;

public class TabControl extends AbstractUIManager {

	private ArrayList<TabItem> mTabItems;
	private ViewGroup mContentView;
	private int mCurIndex;
	private TabWidget mTabWidget;
	boolean isDeal = false;

	private class TabItem {
		public PageControlStack mStack;
		public int mLinkId;
	}
	
	public void setmCurIndex(int mCurIndex) {
		this.mCurIndex = mCurIndex;
	}
	
	@Override
	public void setTabWidget(TabWidget tabWidget) {
		// TODO Auto-generated method stub
		mTabWidget = tabWidget;
	}
	
	@Override
	public Context getContext() {
		// TODO Auto-generated method stub
		return mTabWidget.getContext();
	}
	
	@Override
	public void setTabWidgetHeight(int nHeight) {
		// TODO Auto-generated method stub
		if (mTabWidget != null) {
			mTabWidget.getLayoutParams().height = nHeight;
		}
	}
	
	public TabControl() {
		mCurIndex = -1;
		mTabItems = new ArrayList<TabItem>();
	}

	@Override
	public void gotoTabSection(int nIndex) {

		if (mCurIndex == nIndex) {
			return;
		}

		// tab切换时，上一个tab的page执行onBackground
		AbsUIController curUIController = getCurPageControl();
		if (curUIController != null) {
            curUIController.onBackground();
		}

		mCurIndex = nIndex;
		PageControlStack stack = mTabItems.get(nIndex).mStack;
		gotoPageWithStack(stack, -1);
	}

	@Override
	public void setParentViewGroup(ViewGroup viewGroup) {
		mContentView = viewGroup;
	}

	@Override
	public void gotoPage(int nPageId) {
		PageControlStack pageStack = GetControlStack(nPageId);
		gotoPageWithStack(pageStack, nPageId);
	}

	@Override
	public void gotoPage(EQAction gotoFrameAction) {
		int nPageId = ((EQGotoFrameAction) gotoFrameAction).getGotoFrameId();
		PageControlStack pageControlStack = GetControlStack(nPageId);
		gotoPageWithStack(getCurUIControlStack(), pageControlStack, (EQGotoFrameAction)gotoFrameAction);
	}

	@Override
	public void goBack() {

		PageControlStack stack = getCurUIControlStack();
		if (stack != null) {
            AbsUIController control = stack.goBack();
			if (control != null) {
				recalllayoutControl(control);
			}
		}
	}

	@Override
	public PageControlStack getCurUIControlStack() {

		if (mCurIndex >= 0 && mCurIndex < mTabItems.size()) {
			TabItem item = mTabItems.get(mCurIndex);
			if (item != null) {
				return item.mStack;
			}
		}
		return null;
	}

	@Override
	public void goBackToFirstPage() {
		// TODO Auto-generated method stub
		PageControlStack stack = getCurUIControlStack();
		if (stack != null) {
            AbsUIController uiController = stack.getCurFirstPageControl();
			if (uiController != null) {
				int firstPageId = uiController.getPageId();
				if (firstPageId != 0) {
					gotoPageWithStack(stack, firstPageId);
				}
			}
		}
	}

	private void gotoPageWithStack(PageControlStack lastPageStack, PageControlStack pageStack, EQAction gotoFrameAction) {
		int nPageId = ((EQGotoFrameAction) gotoFrameAction).getGotoFrameId();
        AbsUIController control = null;
		if (pageStack != null) {
			if (nPageId == -1) {
				control = pageStack.getTopUIControl();
				if (control == null) {
					nPageId = mTabItems.get(pageStack.getTabIndex()).mLinkId;
				}
			}

			if (control == null) {
				control = pageStack.getPageControl(lastPageStack, gotoFrameAction);
			}
			mCurIndex = pageStack.getTabIndex();
			if (mTabWidget != null) {
				mTabWidget.requestTabFocus(mCurIndex);
			}
		}
		recalllayoutControl(control);
	}

	private void gotoPageWithStack(PageControlStack pageStack, int nPageId) {

        AbsUIController control = null;
		if (pageStack != null) {
			if (nPageId == -1) {
				control = pageStack.getTopUIControl();
				if (control == null) {
					nPageId = mTabItems.get(pageStack.getTabIndex()).mLinkId;
				}
			}
			if (control == null) {
				control = pageStack.getPageControl(nPageId);
			}
			mCurIndex = pageStack.getTabIndex();
			if (mTabWidget != null) {
				mTabWidget.requestTabFocus(mCurIndex);
			}
		}
		recalllayoutControl(control);
	}

	@Override
	public void recalllayoutControl(AbsUIController control) {
		// TODO Auto-generated method stub
		if (control != null) {
			setContentView(control);
			control.onForeground();
		}
		PublicInterface.refreshNavigateBar();
		PublicInterface.refreshBottomBar();
	}


	private PageControlStack GetControlStack(int nPageId) {
		int nIndex = FindTabbarIndex(nPageId);
		return mTabItems.get(nIndex).mStack;
	}

	private int FindTabbarIndex(int nPageId) {

		if (mCurIndex >= 0) {
			TabItem item = mTabItems.get(mCurIndex);
			if (item != null && item.mStack != null) {
				if (item.mStack.isPageInStackList(nPageId)) {
					return mCurIndex;
				}
			}
		}

		for (int i = 0; i < mTabItems.size(); i++) {
			TabItem item = mTabItems.get(i);
			if (item != null && item.mStack != null) {
				if (item.mStack.isPageInStackList(nPageId)) {
					return i;
				}
			}
		}
		return 0;
	}

	public void setContentView(AbsUIController control) {
		View rootView = control.getRootView();
		ViewGroup parent = (ViewGroup) (rootView.getParent());
		if (parent != null) {
			parent.removeView(rootView);
		}

		if (mContentView != null) {
			mContentView.removeAllViews();
			mContentView.addView(rootView, new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		}
	}

	@Override
	public void init() {
		if (mTabItems == null) {
			mTabItems = new ArrayList<TabItem>();
		}

		for (int i = 0; i < PublicInterface.getTabbarItemCount(); i++) {
			TabberBarItem item = PublicInterface.getTabberBarItemByIndex(i);
			if (item != null) {
				TabItem tab = new TabItem();
				PageControlStack controlStack = new PageControlStack();
				controlStack.setStackId(item.mStack);
				controlStack.setTabIndex(i);

				tab.mStack = controlStack;
				tab.mLinkId = item.mLink;
				controlStack.init(tab.mLinkId);
				mTabItems.add(tab);
			}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		PageControlStack curControlStack = getCurUIControlStack();
		boolean isDeal = false;
		
		if (curControlStack != null) {
			isDeal = curControlStack.getCurPageControl().onKeyDown(keyCode, event);
			
			if (isDeal == false) {
				if (curControlStack.canGoBack()) {
					PublicInterface.goBack();
					isDeal = true;
				}else if (curControlStack.getTabIndex() > 0) {
					PublicInterface.gotoTabbarSection(0);
				} else {
					PublicInterface.exit();
				}
			}
		}
		
		return isDeal;
		
	}
	
	@Override
	public void onDestory() {
		if (mTabItems != null) {
			for (int i = 0; i < mTabItems.size(); i++) {
				TabItem item = mTabItems.get(i);
				if (item != null) {
					PageControlStack pageControlStack = item.mStack;
					if (pageControlStack != null) {
						List<AbsUIController> uiControllerList = pageControlStack.getPageControlList();
						if (uiControllerList != null) {
							for (int j = 0; j < uiControllerList.size(); j++) {
								AbsUIController uiController = uiControllerList.get(j);
								if (uiController != null) {
                                    uiController.onRemove();
								}
							}
						}
						
					}
					
				}
				
			}
		}
		
	}

	@Override
	public AbsUIController getCurPageControl() {
		// TODO Auto-generated method stub
		PageControlStack curPageControlStack = getCurUIControlStack();
		if (curPageControlStack != null) {
			AbsUIController uiController = curPageControlStack.getTopUIControl();
			return uiController;
		}
		return null;
	}
	
	@Override
	public String getCurPageTitle() {
		// TODO Auto-generated method stub
		String title = getCurUIControlStack().getCurPageTitle();
		return title;
	}

	@Override
	public Page getLastPageControl() {
		// TODO Auto-generated method stub
		
		return null;
	}

	@Override
	public boolean isCanGoBack() {
		// TODO Auto-generated method stub
		return getCurUIControlStack().canGoBack();
	}


	
	@Override
	public void close() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onActivityResume() {
		AbsUIController uiController = getCurPageControl();
		if (uiController != null) {
            uiController.onForeground();
		}
	}

	@Override
	public void onActivityPause() {
		AbsUIController uiController = getCurPageControl();
		if (uiController != null) {
            uiController.onBackground();
		}
	}
}
